package com.example.hjt_java_web_homework2.infrastructure.persistence;

import com.example.hjt_java_web_homework2.domain.model.EnvironmentData;
import com.example.hjt_java_web_homework2.domain.repository.EnvironmentDataRepository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author Li Chun Hei
 * @date 31/10/2025
 * @description
 */

@Repository
public interface EnvironmentDataRepositoryImpl extends JpaRepository<EnvironmentData, Long>, EnvironmentDataRepository {

    @Override
    default EnvironmentData save(EnvironmentData environmentData) {
        return saveAndFlush(environmentData);
    }

    @Query("SELECT ed FROM EnvironmentData ed WHERE ed.location = :location ORDER BY ed.recordedAt DESC")
    List<EnvironmentData> findByLocation(@Param("location") String location);


    @Query("SELECT ed FROM EnvironmentData ed WHERE ed.recordedAt BETWEEN :start AND :end ORDER BY ed.recordedAt DESC")
    List<EnvironmentData> findByRecordedAtBetween(@Param("start") LocalDateTime start, @Param("end") LocalDateTime end);

}
